/********************************************************
 *
 * FILE NAME:   filter.cpp
 *
 * DESCRIPTION:
 *  Module with generic signal filter
 *
 ********************************************************/

/********************************************************/
/*              Includes                                */
/********************************************************/
#include "filter.h"

/********************************************************/
/*              Defines                                 */
/********************************************************/

/********************************************************/
/*              Macros                                  */
/********************************************************/

/********************************************************/
/*              Typedefs                                */
/********************************************************/

/********************************************************/
/*              Local Functions Declarations (LOCAL)    */
/********************************************************/

/********************************************************/
/*              Global Variables (GLOBAL/IMPORT)        */
/********************************************************/

/********************************************************/
/*              Local Module Variables (MODULE)         */
/********************************************************/

/********************************************************/
/*              Local File Variables (LOCAL)            */
/********************************************************/

/********************************************************/
/*              Functions Definitions (LOCAL/GLOBAL)    */
/********************************************************/

Filter::Filter(QObject *parent) : QObject(parent){}

bool Filter::FilterOSignal::onBurstRequest(size_t burstLength)
{
    bool result;

    ISignal &x = *contextPtr->input;
    FilterOSignal &y = *this;

    result = x.requestBurst(burstLength);
    if(result == true)
    {
        for(size_t burst = 0; burst < burstLength; ++burst)
        { // TODO unroll for
            x.readInSample();

            y[0] = 0;
            for(size_t i = 0; i < contextPtr->bLength; ++i)
            {
                y[0] += contextPtr->b[i] * x[i];
            }

            for(size_t i = 1; i < contextPtr->aLength; ++i)
            {
                y[0] -= contextPtr->a[i] * y[i];
            }
            y[0] = y[0] / contextPtr->a[0];

            y.writeOutSample();
        }
    }
    return result;
}

ISignal * Filter::getInputSignal()
{
    qDebug() << "---> " << __func__;

    ISignal *result;
    result = input;

    qDebug() << "<--- " << __func__;
    return result;
}

OSignal * Filter::getOutputSignal()
{
    qDebug() << "---> " << __func__;

    OSignal *result;
    result = output;

    qDebug() << "<--- " << __func__;
    return result;
}
